/**
 *
 * @returns 订阅消息Hooks
 */
const useMessage = () => {
  const tmplIds = ['k8fIC3XOqGbD-ge2mnrAdD9K0YxZu3zdEIz0UmoBvdw']
  /**
   *
   * @param success 返回同意订阅的模板id列表
   * @param error 返回报错信息
   */
  const requestMessage = (success: Function, error: Function) => {
    uni.requestSubscribeMessage({
      tmplIds,
      success(res: any) {
        const accepttmplIds: string[] = []
        tmplIds.forEach(tmplId => {
          if (res[tmplId] == 'accept') {
            accepttmplIds.push(tmplId)
          }
        })
        if (accepttmplIds.length > 0) {
          success(accepttmplIds)
        } else {
          error(res)
          uni.showToast({
            title: '订阅失败',
            icon: 'none',
          })
        }
      },
      fail(err) {
        error(err)
        uni.showToast({
          title: '订阅失败',
          icon: 'none',
        })
      },
    })
  }
  return { requestMessage }
}
export default useMessage

// 使用
// import useMessage from '@/hooks/useMessage'
// const { requestMessage } = useMessage()
// function btnTest() {
//   requestMessage(
//     (success: string[]) => {
//       console.log(success)
//     },
//     (error: any) => {
//       console.log('error', error)
//     },
//   )
// }
